﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Earworm.Web.Users
{
    public partial class Account : System.Web.UI.Page
    {

        //connect to database
        Earworm.Web.DBM.EarwormDataContext context = new DBM.EarwormDataContext();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TabContainer_ActiveTabChanged(TabContainer, null);
            }
        }

        protected void TabContainer_ActiveTabChanged(object sender, EventArgs e)
        {
            try
            {
                if (TabContainer.ActiveTabIndex == 0)
                {
                    var user = from User in context.Users where User.Email == "mandrushko@digis.net" select User;
                    foreach (var User in user)
                    {

                        tbFirstName.Text = User.FirstName;
                        tbLastName.Text = User.LastName;
                        tbUserName.Text = User.UserName;
                        tbEmail.Text = User.Email;
                    }
                    //lStatus.Text = "";
                }

                if (TabContainer.ActiveTabIndex == 1)
                {

                }
            }
            catch
            {
                throw;
            }
            finally
            {

            }
        }

        protected void LinqDataSourceRating_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            string userEmail = Session["userEmail"].ToString();
            var users = from user in context.Users where user.Email == userEmail select user;
            foreach (var user in users)
            {
                e.Result = context.sp_my_song_ratings(user.UserKey);
            }
        }

        
        protected void SaveUser_Click(object sender, EventArgs e)
        {
            //string userEmail = Session["userEmail"].ToString();
            //var Resultint = context.sp_change_user_info(1, tbFirstName.Text, tbLastName.Text, tbUserName.Text);
            var Result = from result in context.Users where result.Email == "mandrushko@digis.net" select result;

            
            foreach (var res in Result)
            {
               
                res.LastName = tbLastName.Text;
                res.UserName = tbUserName.Text;
                res.FirstName = tbFirstName.Text;
            }
            context.SubmitChanges();
            /*
            var validUser = context.Users.Single(u => u.Email == "mandrushko.digis.net");
            validUser.FirstName = tbFirstName.Text;
            validUser.LastName = tbLastName.Text;
            validUser.UserName = tbUserName.Text;

            context.SubmitChanges();
            


            /* error checking
            if (Resultint.ToString() == "0")
            {
                lStatus.Text = "Failed!";
            }
            else
            {
                lStatus.Text = "Saved!";
                
            }
             */
             
        }
        

    }

    /*
    public partial class User1 : System.Web.UI.Page
    {
        Earworm.Web.DBM.EarwormDataContext db = new Web.DBM.EarwormDataContext();

        protected void SaveUser_Click(object sender, EventArgs e)
        {

            var user = from User in db.Users where User.Email == "mandrushko@digis.net" select User;

        }
    }
     */
    
}